package com.binge.spring.ioc.factoryBean;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Slf4j
public class MapperFactoryBean implements FactoryBean {

    private SqlSession session;

    private Class aClass;

    public MapperFactoryBean(Class aClass) {
        this.aClass = aClass;
    }

    @Autowired
    public void setSession(SqlSessionFactory sqlSessionFactory) {
        this.session = sqlSessionFactory.openSession();
        sqlSessionFactory.getConfiguration().addMapper(aClass);
        log.info("session:{}", session);
    }

    @Override
    public Object getObject() throws Exception {
       return session.getMapper(aClass);
    }

    @Override
    public Class<?> getObjectType() {
        return aClass;
    }


}
